JavaScript is an interpreted computer programming language. As part of web browsers, implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed. It has also become common in server-side programming, game development and the creation of desktop applications. The synthesis composed of code executed in JavaScript and scope executing the code is called closure, and all of JavaScript functions are closures.
For a closure implemented by an anonymous function, an interface in the closure can not be directly called outside of the closure, and the data within it can not be read or written from outside. Because a function in the closure can not be directly called or a variable value in the closure can not be acquired from outside for a closure implemented by an anonymous function, the closure can not be performed by a white-box testing which is a method of testing software that tests software code from statement and function interfaces layer based on the implementation logic in the code by a tester.
In conventional technology, in order to implement a white-box testing of the closure implemented by an anonymous function, it often needs to adjust the code of the closure to be tested that some interface methods in the closure can be adjusted in a certain way (such as adjusting code) to associate an internal method or data to a externally accessible global object, and this makes the method or data inside the closure can be directly accessed by the code outside the closure, so as to realize a white-box testing. However, using the above method to implement a white-box testing has several problems as follows:
Firstly, the method violates the original purpose of using a closure in sound code, and it makes the benefit brought by the closure disappear, for example, the advantage of hiding internal interface and data is disappeared;
Secondly, because each closure implemented by an anonymous function to be under white-box testing may need to be made an adjustment to the code, the workload of the adjustment depends on the concrete implementation of the closure, and possible restriction of the external test cases runtime environment exists, the workload of the adjustment is heavy;
Thirdly, it needs to have a test of the adjusted closure source code for verifying its function and logic to ensure the program operating properly after the adjustment of the code required for testing, and such test brings additional load for a tester; and
Fourthly, in more extreme cases, for example, the interface or data inside the closure is not allowed to be exposed by the application environment of the closure for security or other reasons, the interface can not be called or acquired by external test code so that the white-box testing can not be implemented.
In conclusion, conventional methods for closure testing are low in testing efficiency and security.